import { message } from "antd";
import { informationInfo, videoInfo, getRightList } from "../services/services";

export default {
  namespace: "add",

  state: {
    articleData: [
      //多个文稿数据集合
    ],
    gltjData: [], //关联推荐 视频
    currentActiveArticle: 1, //当前选中文稿
    dataSource: {}, //数据列表
    dataSourceRef: [],
    informationInfo: {}, //文章图集详情（编辑）
    videoInfo: {}, //视频音频详情（编辑）
    mchType: "editor", //状态（编辑或新增）
    activeKey: "1", //当前类型
    allDataSource: [{ title: "新建稿件", content: {}, key: "1" }],
    configJson: {},
    transcode_data: [], //转码数据
    spinning: false,
    editorRow: {},
    rightList: [],
    receive_site_ids: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
      // eslint-disable-line
    }
  },

  effects: {
    /**
     * 获取文章图集详情
     */
    *editinfo({ data, callback }, { call, put, select }) {
      const list = yield call(informationInfo, {
        ...data
      });
      if (list.code === 200) {
        yield put({ type: "saveInformationInfo", data: list.data });
      }
      if (callback) callback(list.data);
    },
    /**
     * 获取视频音频详情
     */
    *editvideo({ data, callback }, { call, put, select }) {
      const list = yield call(videoInfo, {
        ...data
      });
      if (list.code === 200) {
        yield put({ type: "saveVideoInfo", data: list.data });
      }
      if (callback) callback(list.data);
    },
    *getQuanxian({ payload }, { call, put, select }) {
      let str = yield call(getRightList, payload.data);
      if (str.code === 200) {
        let arr = str.data.service_access || [];
        let configJson = str.data.tenant_info.config_json ? str.data.tenant_info.config_json.upload_config : {};
        let receiveSiteIds = str.data.receive_site_ids || [];
        yield put({ type: "updateQuanxian", data: arr });
        yield put({ type: "configJson", data: configJson });
        yield put({ type: "setReceiveSiteIds", data: receiveSiteIds });
      } else if (str.data.code === 500) {
        message.error(str.msg);
      }
    }
  },

  reducers: {
    setSpin(state, action) {
      return { ...state, spinning: action.data };
    },
    setStyleLibrary(state, action) {
      return { ...state, styleLibrary: action.data };
    },
    saveAllDataSource(state, action) {
      return { ...state, allDataSource: action.data };
    },
    saveMchType(state, action) {
      return { ...state, mchType: action.data };
    },
    saveInformationInfo(state, action) {
      return { ...state, informationInfo: action.data };
    },
    saveVideoInfo(state, action) {
      return { ...state, videoInfo: action.data };
    },
    setActiveKey(state, action) {
      return { ...state, activeKey: action.data };
    },
    setEditorRow(state, action) {
      return { ...state, editorRow: action.data };
    },
    select(state, action) {
      return { ...state, currentActiveArticle: action.data.id };
    },
    delete(state, action) {
      //header头直接删除
      let data = state.articleData;
      let currentActiveArticle = state.currentActiveArticle;
      if (data.length === 1) {
        message.info("最后一个稿件不能删除");
        return { ...state };
      }
      let key = action.data.key;
      if (data[key].id === currentActiveArticle) {
        if (data.length === key + 1) {
          //如果当前选中的为最后一个稿件，那么选中前一个
          currentActiveArticle = data[key - 1].id;
        } else {
          //否则如果为第一个稿件，那么选中后一个
          currentActiveArticle = data[key + 1].id;
        }
      }
      data.splice(action.data.key, 1);
      return { ...state, articleData: data, currentActiveArticle: currentActiveArticle };
    },
    setFields(state, action) {
      let data = state.articleData;
      return { ...state, articleData: data };
    },
    configJson(state, action) {
      return { ...state, configJson: action.data };
    },
    setReceiveSiteIds(state, action) {
      return { ...state, receive_site_ids: action.data };
    },
    setTranscodeData(state, action) {
      return { ...state, transcode_data: action.data };
    },
    updateQuanxian(state, action) {
      return { ...state, rightList: action.data };
    }
  }
};
